package web.servlet;

import javax.servlet.http.HttpServlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import domain.Cart;
import domain.CartItem;

import java.io.IOException;
import java.util.Iterator;


public class changeCartServlet extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HttpSession session = request.getSession();
        Cart cart = (Cart) session.getAttribute("cart");
        Iterator<CartItem> cartItems = cart.getAllCartItems();

        response.setContentType("text/html");
        String q=request.getParameter("quantity").toString();
        String itemId=request.getParameter("id").toString();
        int quantity=Integer.parseInt(q);

        System.out.println(itemId);
        System.out.println(quantity);
        cart.setQuantityByItemId(itemId,quantity);

        response.getWriter().write("{\"data1\":\"" + cart.getTotalByItemId(itemId).toString() + "\",\"data2\":\"" + cart.getSubTotal().toString() + "\"}");

    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        this.doGet(request, response);
    }
}
